﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using BLL;
using Utils;

namespace UI.ModSalary
{
    public partial class EmpItemChangeBatch : System.Web.UI.Page
    {
        IBizSQL bizSQL = new BizSQL();
        DefDicBiz defDicBiz = new DefDicBiz();
        SetsItemBiz setsItemBiz = new SetsItemBiz();
        EmpItemChangeBiz empItemChangeBiz = new EmpItemChangeBiz();
        string account = "";
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                defDicBiz.InitXMLDropDownList(ddlMonth, "MonthList.xml", (System.DateTime.Now.Month - 1).ToString());
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                DataTable dt = setsItemBiz.GetItemDict("1", "part");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlItem.Items.Add(new ListItem(dt.Rows[i]["ItemName"].ToString(), dt.Rows[i]["WageItem"].ToString()));
                }
                ddlItem.SelectedValue = "职补";
            }
        }
        protected void ddlSetsID_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setsID = ddlSetsID.SelectedValue;
            if (setsID == "0") return;

            DataTable dt = setsItemBiz.GetItemDict(setsID, "part");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ddlItem.Items.Add(new ListItem(dt.Rows[i]["ItemName"].ToString(), dt.Rows[i]["WageItem"].ToString()));
            }
        }
        //查询 指定月份转正的人，加薪，调职，晋升的人
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string setsID = ddlSetsID.SelectedValue;
            string wageItem = ddlItem.SelectedValue;
            string strMonth = ddlMonth.SelectedValue;
            if (setsID == "0" || wageItem == "0")
            {
                JSUtility.Alert(this, "请选择帐套和工资项目!");
                return;
            }
            DataTable dt = empItemChangeBiz.GetEmpList(wageItem, strMonth,"all");
            gvList.DataSource = dt;
            gvList.DataBind();
        }

        protected void btnModi_Click(object sender, EventArgs e)
        {
            string wageItem = ddlItem.SelectedValue;
            string strMonth = ddlMonth.SelectedValue;
            if (wageItem == "0")
            {
                JSUtility.Alert(this, "请选择工资项目!");
                return;
            }
            StringBuilder builder = new StringBuilder();   
            for (int i = 0; i < gvList.Rows.Count; i++)
            {
                GridViewRow row = gvList.Rows[i];

                string empCode = ((Label)row.Cells[0].FindControl("lbEmpCode")).Text;
                //string empName = ((Label)row.Cells[0].FindControl("lbEmpName")).Text;
                string wageNum = ((Label)row.Cells[0].FindControl("lbWageNum")).Text;                
                string newWageNum = ((TextBox)row.Cells[0].FindControl("txtNewWageNum")).Text;
                string typeName = ((Label)row.Cells[0].FindControl("lbTypeName")).Text;
                if (!RegexRule.IsDecimal(newWageNum)) continue;
                if (empCode == "") continue;
                if (decimal.Parse(wageNum) == decimal.Parse(newWageNum)) continue;
                builder.Append("Update Wage_EmpItem Set " + wageItem + "=" + newWageNum+" Where EmpCode='"+empCode+"';");
                builder.Append("Insert Into  Wage_Change (EmpCode,ChangeTypeName,ChangeTime,SetsID,WageItem,ItemName,OldAllowance,NewAllowance,Account) values ('" + empCode + "','" + typeName + "','" + System.DateTime.Now.Year + "-"+StringUtility.LeftAppend(strMonth,2)+"-01'," + ddlSetsID.SelectedValue + ",'" + wageItem + "','" + ddlItem.SelectedItem.Text + "'," + wageNum + "," + newWageNum + ",'" + account + "');");
               }
            //填空白
            builder.Append("update a set a.EmpName=b.EmpName,a.DutyName=b.DutyName,a.DeptName=b.DeptName,a.ZipCode=b.ZipCode,a.StateName=b.StateName from Wage_Change a, View_Emp b where a.EmpCode=b.EmpCode and (a.EmpName is null); ");
            if (bizSQL.ExecSQL(builder.ToString()))
            { JSUtility.Alert(this, "执行成功!"); }
            else { JSUtility.Alert(this, "执行失败!"); }
          
        }
    }
}